package com.marathon.ershor.textrecognizer;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by ershor on 2017/8/17.
 */

public class OKHttpUploader {
    private static OkHttpClient mClient = new OkHttpClient();

    public static String recognizeImage(File file, int x, int y, int confidence) {
        String jsonResult = "";
        try {
            RequestBody requestBody = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("file", file.getName(),
                            RequestBody.create(MediaType.parse("image/jpeg"), file))
                    .addFormDataPart("x", Integer.toString(x))
                    .addFormDataPart("y", Integer.toString(y))
                    .addFormDataPart("confidence", Integer.toString(confidence))
                    .build();

            Request request = new Request.Builder()
                    .url("http://10.144.89.87:61905/hackthon/yolo")
                    .post(requestBody)
                    .build();

            Response response = mClient.newCall(request).execute();
            jsonResult = response.body().string();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return jsonResult;
    }
}
